package cn.com.utt.gateway.controller;

import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.com.utt.common.service.SendEmailService;
import cn.com.utt.common.service.UserService;
import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.User;

/**
 * @author zhang.xiaoyan
 * @date 2017年9月13日
 */
@RestController
@RequestMapping("v1")
public class SendCodeController {
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;

	@Autowired
	private SendEmailService sendEmailService;

	@Autowired
	private UserService userService;

	@RequestMapping(value = "sendcode", method = { RequestMethod.POST })
	public Map<String, Object> sendCode(String email, HttpSession session) {
		RestResult result = new RestResult();
		if (email == null) {
			result.setErrorCode(-1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		User user = userService.queryByUsernameOrEmail(email);
		if (user != null) {
			// 已经注册过
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PhoneExist));
			return result.toJsonObject();
		}
		int code = (int) (Math.random() * 9000 + 1000);
		boolean err = sendEmailService.sendEmail(email, "Registration", "Your verification code:"+code);
		System.out.println(err);
		if (err) {
			result.setErrorCode(0);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.SMSSent));
			session.setAttribute("code" + email, code);
			session.setMaxInactiveInterval(31 * 60);// 设置session的过期时间为30分钟，即code有效期为30分钟
		} else {
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.SMSSentFailed));
		}
		return result.toJsonObject();
	}

	@RequestMapping(value = "sendforgetcode", method = { RequestMethod.POST })
	public Map<String, Object> sendForgetCode(String email, HttpSession session) {
		RestResult result = new RestResult();
		if (email == null) {
			result.setErrorCode(-1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		User user = userService.queryByUsernameOrEmail(email);
		if (user == null) {
			// 未注册过
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.NoUsername));
			return result.toJsonObject();
		}
		int code = (int) (Math.random() * 9000 + 1000);
		boolean err = sendEmailService.sendEmail(email, "Forget", "Your verification code:"+code);
		if (err) {
			result.setErrorCode(0);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.SMSSent));
			session.setAttribute(email, code);
			session.setMaxInactiveInterval(31 * 60);// 设置session的过期时间为30分钟，即code有效期为30分钟
		} else {
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.SMSSentFailed));
		}
		return result.toJsonObject();
	}

	@RequestMapping(value = "sendupdatecode", method = { RequestMethod.POST })
	public Map<String, Object> sendUpdateCode(String email, HttpSession session) {
		RestResult result = new RestResult();
		if (email == null) {
			result.setErrorCode(-1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		User user = userService.queryByUsernameOrEmail(email);
		if (user != null) {
			// 已经注册过
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PhoneExist));
			return result.toJsonObject();
		}
		int code = (int) (Math.random() * 9000 + 1000);
		boolean err = sendEmailService.sendEmail(email, "Update", "Your verification code:"+code);
		if (err) {
			result.setErrorCode(0);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.SMSSent));
			session.setAttribute("updatecode" + email, code);
			session.setMaxInactiveInterval(31 * 60);// 设置session的过期时间为30分钟，即code有效期为30分钟
		} else {
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.SMSSentFailed));
		}
		return result.toJsonObject();
	}

}